package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.TravelPaper;
import cn.wolfcode.trip.base.query.TravelPaperQuery;
import cn.wolfcode.trip.base.service.ITravelPaperService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("travelPaper")
public class TravelPaperController {

    @Autowired
    private ITravelPaperService travelPaperService;


    @RequestMapping("list")
    public String list(@ModelAttribute("qo") TravelPaperQuery qo, Model model){
        qo.setOrderBy("t.releaseTime desc");
        model.addAttribute("result",travelPaperService.selectForList(qo));
        return "travelPaper/list";
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(TravelPaper travelPaper, MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                //String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PAGE + "upload");
                String  imgUrl= SevenUploadUtil.uploadQiniu(file);
                travelPaper.setCoverUrl(imgUrl);
            }
            travelPaperService.saveOrUpdate(travelPaper);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("selectByTravelPaperId")
    @ResponseBody
    public TravelPaper selectByTravelPaperId(Long travelPaperId){
        TravelPaper travelPaper = travelPaperService.selectByTravelPaperId(travelPaperId);
        return travelPaper;
    }
}
